package com.ie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ie.common.KaptchaServiceUtils;
import com.ie.entity.Admins;
import com.ie.entity.Users;
import com.ie.entity.dto.UsersDto;
import com.ie.entity.vo.UserVo;
import com.ie.mapper.AdminsMapper;
import com.ie.mapper.UsersMapper;
import com.ie.service.UsersService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.awt.image.BufferedImage;
import java.util.Objects;

/**
* @author admin
* @description 针对表【users】的数据库操作Service实现
* @createDate 2025-03-02 22:16:12
*/
@Service
public class UsersServiceImpl
    implements UsersService {

    @Autowired
    private UsersMapper usersMapper;

    @Autowired
    private AdminsMapper adminsMapper;

    @Autowired
    private KaptchaServiceUtils kaptchaServiceUtils;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    @Override
    public void registerUser(UsersDto usersDto) {
        QueryWrapper<Users> username = new QueryWrapper<Users>().eq("username", usersDto.getUsername());
        if (usersMapper.selectOne(username) != null) {
            throw new RuntimeException("用户名已存在");
        }

        Users users = new Users();
        BeanUtils.copyProperties(usersDto, users);
        users.setPasswd(DigestUtils.md5DigestAsHex(usersDto.getPasswd().getBytes()));
        usersMapper.insert(users);
    }

    @Override
    public UserVo loginUser(UsersDto usersDto) {
        UserVo userVo = new UserVo();
        if (usersDto.getUserType().equals("0")) {
            QueryWrapper<Users> username = new QueryWrapper<Users>().eq("username", usersDto.getUsername());
            Users users = usersMapper.selectOne(username);
            if (users == null) {
                throw new RuntimeException("用户名不存在");
            }
            String key = DigestUtils.md5DigestAsHex(usersDto.getPasswd().getBytes());
            System.out.println("key = " + key);
            if (!Objects.equals(key, users.getPasswd())) {
                throw new RuntimeException("密码错误");
            }
            BeanUtils.copyProperties(users, userVo);

        } else {
            QueryWrapper<Admins> username = new QueryWrapper<Admins>().eq("accountname", usersDto.getUsername());
            Admins admins = adminsMapper.selectOne(username);
            if (admins == null) {
                throw new RuntimeException("用户名不存在");
            }
            String key = DigestUtils.md5DigestAsHex(usersDto.getPasswd().getBytes());
            System.out.println("key = " + key);
            if (!Objects.equals(key, admins.getPasswd())) {
                throw new RuntimeException("密码错误");
            }

            userVo.setId(admins.getId());
            userVo.setUsername(admins.getAccountname());
            userVo.setPasswd(admins.getPasswd());
            userVo.setIdCard(admins.getIdCard());
            userVo.setRealName(admins.getRealName());

        }
        return userVo;
    }

    @Override
    public BufferedImage getCode() {
        String captchaText = kaptchaServiceUtils.createCaptchaText();
        BufferedImage captchaImage = kaptchaServiceUtils.createCaptchaImage(captchaText);
        return captchaImage;
    }

    @Override
    public void ifCode(UsersDto usersDto) {

    }

    @Override
    public void updateUser(UsersDto usersDto) {
        UpdateWrapper<Users> id = new UpdateWrapper<Users>().eq("id", usersDto.getId());
        Users users = new Users();
        BeanUtils.copyProperties(usersDto, users);
        usersMapper.update(users, id);
    }

    @Override
    public UserVo getUserMessage(Integer id) {
        QueryWrapper<Users> id1 = new QueryWrapper<Users>().eq("id", id);
        Users users = usersMapper.selectOne(id1);
        UserVo userVo = new UserVo();
        BeanUtils.copyProperties(users, userVo);
        return userVo;
    }
}




